<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改电影信息管理')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-movie-edit" th:object="${movie}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">电影名：</label>
                <div class="col-sm-8">
                    <input name="name" th:field="*{name}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">简介：</label>
                <div class="col-sm-8">
                    <textarea name="info" class="form-control">[[*{info}]]</textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">分类：</label>
                <div class="col-sm-8" th:with="type=${@dict.getType('movie_type')}">
                    <label th:each="dict : ${type}" class="check-box">
                        <input name="type" type="checkbox" th:value="${dict.dictValue}" th:text="${dict.dictLabel}" th:attr="checked=${movie.type.contains(dict.dictValue)?true:false}">
                    </label>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">导演：</label>
                <div class="col-sm-8">
                    <input name="direct" th:field="*{direct}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">演员：</label>
                <div class="col-sm-8">
                    <input name="starts" th:field="*{starts}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">发布时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="publicTime" th:value="${#dates.format(movie.publicTime, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">时长：</label>
                <div class="col-sm-8">
                    <input name="runtime" th:field="*{runtime}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">封面：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="img" th:field="*{img}">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="img" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">剧照：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="photos" th:field="*{photos}">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="photos" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">评分：</label>
                <div class="col-sm-8">
                    <input name="rate" th:field="*{rate}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">点击数：</label>
                <div class="col-sm-8">
                    <input name="count" th:field="*{count}" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "system/movie";
        $("#form-movie-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-movie-edit').serialize());
            }
        }

        $("input[name='publicTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("#img").on("change",function (i) {
            var val = $("input[name='" + this.id + "']").val()
            $(this).fileinput({
                'uploadUrl': ctx + 'common/upload',
                initialPreviewAsData: true,
                initialPreview: [val],
                maxFileCount: 1,
                autoReplace: true
            }).on('fileuploaded', function (event, data, previewId, index) {
                $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            })
            $(this).fileinput('_initFileActions');
        });
        var List = new Array();
        $(".file-upload").each(function (i) {
            if(i==0){
                var val = $("input[name='" + this.id + "']").val()
                $(this).fileinput({
                    'uploadUrl': ctx + 'common/upload',
                    initialPreviewAsData: true,
                    initialPreview: [val],
                    maxFileCount: 1,
                    autoReplace: true
                }).on('fileuploaded', function (event, data, previewId, index) {
                    $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
                }).on('fileremoved', function (event, id, index) {
                    $("input[name='" + event.currentTarget.id + "']").val('')
                })
                $(this).fileinput('_initFileActions');
            }else{
                var val = $("input[name='" + this.id + "']").val()
                var valarr = val.split(",")
                $(this).fileinput({
                    'uploadUrl': ctx + 'common/upload',
                    initialPreviewAsData: true,
                    initialPreview: valarr,
                    maxFileCount: 10,
                }).on('fileuploaded', function (event, data, previewId, index) {
                    List.push({ FileName: data.response.url, KeyID: previewId })
                    const fileNamesArray = List.map(item => item.FileName);

                    const resultString = fileNamesArray.join(',');
                    $("input[name='" + event.currentTarget.id + "']").val(resultString)
                    var url1 = $("input[name='" + event.currentTarget.id + "']").val()
                    console.log(url1)
                }).on('filesuccessremove', function (event, data,id, index) {
                    for (var i = 0; i < List.length; i++) {
                        if (List[i].KeyID == data) {
                            List.splice(i,1)
                        }
                    }
                    const fileNamesArray = List.map(item => item.FileName);

                    const resultString = fileNamesArray.join(',');
                    $("input[name='" + event.currentTarget.id + "']").val(resultString)
                    var url1 = $("input[name='" + event.currentTarget.id + "']").val()
                    console.log(url1)                })
                $(this).fileinput('_initFileActions');
            }

        });
    </script>
</body>
</html>